<template>
  <el-dialog title="新增员工" @close="cancelDialog" :visible="showDialog">
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      style="width:75%; margin-left:50px;text-align:left"
      label-width="80px"
    >
      <el-form-item prop="username" label="姓名">
        <el-input
          v-model="form.username"
          placeholder="请输入姓名"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item prop="mobile" label="手机号">
        <el-input
          v-model="form.mobile"
          placeholder="请输入手机号"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker
          type="date"
          v-model="form.timeOfEntry"
          style="width:100%"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select
          style="width:100%"
          v-model="form.formOfEmployment"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input
          v-model="form.workNumber"
          placeholder="请输入工号"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input
          @focus="showTreeData"
          @blur="handleBlur"
          v-model="form.departmentName"
          autocomplete="off"
        ></el-input>
        <!-- 放一个tree来显示部门 -->
        <el-tree
          v-if="treeShow"
          v-loading="treeLoading"
          @node-click="nodeClick"
          default-expand-all
          :data="departments"
          :props="defaultProps"
        ></el-tree>
      </el-form-item>

      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          type="date"
          v-model="form.correctionTime"
          prop=""
          style="width:100%"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align:center" class="dialog-footer">
      <el-button @click="cancelDialog">取 消</el-button>
      <el-button type="primary" @click="addSave">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 获取部门信息
import { getGepartment } from "@/api/department";
import { transToTreeData } from "@/utils";
// 员工
import { addEmple } from "@/api/employees";
// 导入枚举库
import enumObj from "@/api/constant/employees";
export default {
  name: "addEmp",
  props: {
    showDialog: {
      type: Boolean,
      require: false
    }
  },

  data() {
    return {
      hireType: enumObj.hireType,
      // 树形控件绑定数据
      departments: [],
      // 指定树的文字和子节点是什么属性
      defaultProps: {
        label: "name",
        children: "children"
      },
      // 控制树形控件是否显示的属性
      treeShow: false,
      treeLoading: false,
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        timeOfEntry: "",
        correctionTime: "",
        departmentName: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入员工姓名", trigger: "blur" }
        ],

        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" }
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" }
        ],
        workNumber: [
          { required: true, message: "请输入员工工号", trigger: "blur" }
        ],
        departmentName: [
          { required: true, message: "请选择部门", trigger: "change" }
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 部门文本框获得焦点事件
    async showTreeData() {
      this.treeShow = true;
      this.treeLoading = true;
      let res = await getGepartment();
      this.departments = transToTreeData(res.data.depts, "");
      this.treeLoading = false;
    },
    // 树形图点击事件
    nodeClick(data, node, tree) {
      this.form.departmentName = data.name;
      this.treeShow = false;
    },
    // 失去焦点进行判断是否点击的tree树
    handleBlur(e) {
      if (e.relatedTarget.className.indexOf("el-tree-node") == -1) {
        this.treeShow = false;
      }
    },
    // 取消隐藏
    cancelDialog() {
      this.form = {
        username: "",
        mobile: "",
        timeOfEntry: "",
        formOfEmployment: "",
        workNumber: "",
        correctionTime: "",
        departmentName: ""
      };
      this.$refs.form.resetFields();
      this.$emit("update:showDialog", false);
    },
    addSave() {
      // 做整个表单验证
      this.$refs.form.validate(async v => {
        if (v) {
          await addEmple(this.form);
          this.$message.success("新增成功！");
          let res = this.$parent.pageInfo;
          res.total += 1;
          let last = Math.ceil(res.total / res.size);
          this.$parent.pageInfo.page = last;
          // 隐藏对话框
          this.$emit("update:showDialog", false);
          // 父组件要刷新列表
          this.$parent.getList();
        }
      });
    }
  }
};
</script>
